package com.iraeta.service.basic.impl;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import com.iraeta.dao.main.ErpCangkuDao;
import com.iraeta.dao.main.ErpCangkuUserDao;
import com.iraeta.dao.main.ErpCurrentStoreDao;
import com.iraeta.entity.main.ErpCangku;
import com.iraeta.entity.main.ErpCangkuUser;
import com.iraeta.entity.main.User;
import com.iraeta.service.basic.CangKuService;
import com.iraeta.uitl.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class CangKuServiceImpl implements CangKuService {
    @Autowired
    ErpCangkuDao erpCangkuDao;
    @Autowired
    ErpCurrentStoreDao erpCurrentStoreDao;
    @Autowired
    ErpCangkuUserDao erpCangkuUserDao;

    private static final TimedCache<String, List<ErpCangku>> whCache = CacheUtil.newTimedCache(30 * 60 * 1000);

    @Override
    public int addCangKu(ErpCangku record) {
        return erpCangkuDao.insert(record);
    }

    @Override
    public int updateCangKu(ErpCangku record) {
        return erpCangkuDao.updateByPrimaryKey(record);
    }

    @Override
    public ErpCangku getByPrimaryKey(Integer id) {
        return erpCangkuDao.selectByPrimaryKey(id);
    }

    @Override
    public ErpCangku getByName(String cwhname) {
        return erpCangkuDao.selectByName(cwhname);
    }

    @Override
    public ErpCangku getByCode(String cwhcode) {
        return erpCangkuDao.selectByCode(cwhcode);
    }

    @Override
    public void updateAllCangKu() {
        List<HashMap<String,Object>> mapWhList = erpCurrentStoreDao.listWh();
        for (HashMap<String, Object> whMap : mapWhList) {
            String cWhCode = whMap.get("cWhCode").toString();
            String cWhName = whMap.get("cWhName").toString();
            ErpCangku erpCangkuDb = this.getByCode(cWhCode);
            if(erpCangkuDb == null){
                erpCangkuDb = new ErpCangku(cWhCode ,cWhName, 0, 0);
                this.addCangKu(erpCangkuDb);
                whCache.clear();
            }else{
                if(!erpCangkuDb.getCwhname().equals(cWhName)){ //名字修改了
                    erpCangkuDb.setCwhname(cWhName);
                    this.updateCangKu(erpCangkuDb);
                    whCache.clear();
                }
            }
        }
    }

    @Override
    public List<ErpCangku> listErpCangku(Integer userId) {
        return erpCangkuDao.selectUserCangKu(userId);
    }

    @Override
    public List<ErpCangku> listPlanErpCangku(Integer planId) {
        return erpCangkuDao.listPlanErpCangku(planId);
    }

    @Override
    public List<ErpCangku> listErpCangku() {
        List<ErpCangku> erpCangkuListDb =  whCache.get("wh");
        if(erpCangkuListDb != null ){
            return erpCangkuListDb;
        }else {
            List<ErpCangku> erpCangkuList = erpCangkuDao.selectAllCangKu();
            whCache.put("wh", erpCangkuList);
            return erpCangkuList;
        }
    }

    @Override
    public List<ErpCangku> searchErpCangku(String cWhName) {
        return erpCangkuDao.selectListByName(cWhName);
    }

    /*仓库填报员维护*/
    @Override
    public List<User> listCangKuUser(Integer cangkuId) {
        return erpCangkuUserDao.listCangKuUser(cangkuId);
    }

    @Override
    public void saveCangKuUser(Integer cangkuId, List<Integer> users) {
        for (Integer userId : users) {
            if(erpCangkuUserDao.selectByCIdAndUid(cangkuId, userId) == null){
                ErpCangkuUser erpCangkuUser = new ErpCangkuUser(cangkuId, userId);
                erpCangkuUserDao.insert(erpCangkuUser);
            }
        }
        String userIdStr = Util.parseListToString(users);
        erpCangkuUserDao.deleteCangKuUserIdStr(cangkuId, userIdStr);
    }
}
